﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_PartPrint : Form
    {
        #region 定义全局对象及变量
        int page = 0;//打印指定的页
        private DataGridView dgv = new DataGridView();
        private string name = "";
        private string start = "";
        private string end = "";
        private int intPage = 0;//总页数
        private int startNum = 0;//开始条数
        private int endNum = 0;//结束条数
        private string type = "";
        #endregion
        public Form_PartPrint(DataGridView _dgv, string _name, string _start, string _end, string _type)
        {
            InitializeComponent();
            dgv = _dgv;
            name = _name;
            start = _start;
            end = _end;
            type = _type;
            intPage = (int)Math.Ceiling(Convert.ToDouble(dgv.Rows.Count) / 20);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int intStart = 0;
                int intEnd = 0;
                if (radioButton2.Checked)
                {
                    if (string.IsNullOrEmpty(textBox2.Text))
                    {
                        MessageBox.Show("请指定要打印的页码！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else if (textBox2.Text.IndexOf(",") == -1)
                    {
                        if (textBox2.Text.IndexOf("-") == -1)
                        {
                            page = Convert.ToInt32(textBox2.Text);
                            if (page > intPage)
                            {
                                MessageBox.Show("要打印的页码超过了文档的最大页，请重新设置！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                textBox2.Text = "";
                                textBox2.Focus();
                                return;
                            }
                            intStart = page;
                            intEnd = page;
                        }
                        else
                        {
                            string[] strSubPages = textBox2.Text.Split('-');
                            intStart = Convert.ToInt32(strSubPages[0].ToString());
                            intEnd = Convert.ToInt32(strSubPages[1].ToString());
                            if (intStart > intPage || intEnd > intPage)
                            {
                                MessageBox.Show("要打印的页码超过了文档的最大页，请重新设置！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                textBox2.Text = "";
                                textBox2.Focus();
                                return;
                            }
                        }
                    }
                    else
                    {
                        string[] strPages = textBox2.Text.Split(',');
                        for (int i = 0; i < strPages.Length; i++)
                        {
                            if (strPages[i].IndexOf("-") == -1)
                            {
                                int page = Convert.ToInt32(strPages[i]);
                                if (page > intPage)
                                {
                                    MessageBox.Show("要打印的页码超过了文档的最大页，请重新设置！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    textBox2.Text = "";
                                    textBox2.Focus();
                                    return;
                                }
                                intStart = page;
                                intEnd = page;
                            }
                            else
                            {
                                string[] strSubPages = strPages[i].Split('-');
                                intStart = Convert.ToInt32(strSubPages[0].ToString());
                                intEnd = Convert.ToInt32(strSubPages[1].ToString());
                                if (intStart > intPage || intEnd > intPage)
                                {
                                    MessageBox.Show("要打印的页码超过了文档的最大页，请重新设置！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    textBox2.Text = "";
                                    textBox2.Focus();
                                    return;
                                }
                            }
                        }
                    }
                    if (intStart > 0)
                    {                        
                        startNum = (intStart - 1) * 21;
                    }
                    else
                    {
                        startNum = 0;
                    }
                    if (intEnd > 0)
                    {  
                        if (intEnd == intPage)
                            endNum = dgv.Rows.Count;
                        else if (intEnd == intStart)
                        {
                            endNum = startNum + 21;
                        }
                        else
                            endNum = intEnd * 21;
                    }
                    else
                    {
                        endNum = 0;
                    }                    
                }
                if (endNum > 0)
                {
                    DataGridView newDgv = new DataGridView();
                    for (int a = 0; a < dgv.Columns.Count; a++)
                    {
                        newDgv.Columns.Add(dgv.Columns[a].Name, dgv.Columns[a].HeaderText);
                    }
                    int cnt = 0;
                    for (int i = startNum; i < endNum; i++)
                    {
                        newDgv.Rows.Add();
                        for (int j = 0; j < dgv.Rows[i].Cells.Count; j++)
                        {
                            newDgv.Rows[cnt].Cells[j].Value = dgv.Rows[i].Cells[j].Value;
                        }
                        cnt++;
                    }
                    Common.PrintDataGridView.Print(newDgv, "日出票报表明细(" + type + ")", "制表人：" + name + "，开票时间:" + start + "——" + end, startNum, endNum);
                }
                else
                {
                    Common.PrintDataGridView.Print(dgv, "日出票报表明细(" + type + ")", "制表人：" + name + "，开票时间:" + start + "——" + end, startNum, endNum);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        //标识全部打印
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                textBox2.Enabled = false;
                startNum = 0;
                endNum = 0;
            }

        }

        //标识打印指定页
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Enabled)
            {
                textBox2.Enabled = true;
                textBox2.Focus();
            }
        }
    }
}
